import {createSlice} from '@reduxjs/toolkit'
import axios from 'axios'
const shopcartSlice=createSlice({
    name:'shopcartNS',
    initialState:{
        shopcartList:[]
    },
    reducers:{
        getShopCartList(state,action){
            state.shopcartList=action.payload
        }
    }
})
//异步操作
export const getShopCartListAsync=async(dispatch,getState)=>{
    console.log('----------异步获取购物车列表信息------------');
    const result=await axios.get('http://www.zhaijizhe.cn:3001/shopcart/getShopcartData')
    console.log('result',result.data.data);
    dispatch(getShopCartList(result.data.data))

}

export const changeShopCartNumAsync=(payload)=>{
    return async(dispatch,getState)=>{
        console.log('----------异步的更新购物车的数量------------');
        console.log('payload',payload);
        const result=await axios.post('http://www.zhaijizhe.cn:3001/shopcart/changeNum',payload)
        console.log('result',result);
        if(result.data.code) dispatch(getShopCartListAsync)
    }
}
export const {getShopCartList}=shopcartSlice.actions
export default shopcartSlice.reducer